package com.business.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.system.mapper.AttentionMapper;
import com.business.system.service.IAttentionService;
import com.common.entity.Attention;
import com.common.untils.CommonResult;
import jdk.internal.org.objectweb.asm.util.TraceAnnotationVisitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * TODO
 *
 * @author lenovo
 * @version 1.0
 * @since 2022-09-23  11:09:55
 */
@Service
public class AttentionServeImpl implements IAttentionService {
    @Autowired
    private AttentionMapper attentionMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public CommonResult inset(Integer sid,String name,String hometoken) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(hometoken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        Integer id = client.getInteger("id");
            Attention attention=new Attention();
            attention.setSId(sid);
            attention.setName(name);
            attention.setCId(id);
            int insert = attentionMapper.insert(attention);
            if (insert == 1) {
                return new CommonResult(2000,"关注成功",null);
            }
            return new CommonResult(5000,"关注失败",null);


    }

    @Override
    public CommonResult delete(Integer sid) {
        QueryWrapper<Attention> wrapper=new QueryWrapper();
        wrapper.eq("s_id",sid);
        int delete = attentionMapper.delete(wrapper);
        System.out.println(delete+"-------------------");
        if (delete == 1) {
            return new CommonResult(2000,"取消关注成功",null);
        }
        return new CommonResult(5000,"取消关注失败",null);
    }

    @Override
    public CommonResult select(Integer currentPage,Integer pageSize,String hometoken) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(hometoken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        Integer id = client.getInteger("id");
        Page<Attention> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Attention> wrapper=new QueryWrapper<>();
        wrapper.eq("a.c_id",id);
        IPage<Map> iPage = attentionMapper.selectByCid(page,wrapper);
        return new CommonResult(2000,"查询成功",iPage);
    }

    @Override
    public CommonResult selectDP(Integer sid) {
        //根据sid查询中间有没有这个店铺
        QueryWrapper<Attention> wrapper=new QueryWrapper<>();
        wrapper.eq("s_id",sid);
        Attention attention = attentionMapper.selectOne(wrapper);
        if (attention == null) {
            return new CommonResult(5000,"已经关注该店铺了",null);
        }

        return new CommonResult(2000,"请关注关注该店铺了",null);
    }
}
